package com.zh.business.service.web.user.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.zhbootcommon.entity.user.LevelInfo;
import com.zh.business.mapper.user.DLevelInfoMapper;
import com.zh.business.service.web.user.IDLevelInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * VIp等级表 服务实现类
 * </p>
 *
 * @author JueDream
 * @since 2021-08-04
 */
@Service
public class DLevelInfoServiceImpl extends ServiceImpl<DLevelInfoMapper, LevelInfo> implements IDLevelInfoService {

    @Autowired
    DLevelInfoMapper levelInfoMapper;

    @Override
    public Page<LevelInfo> queryLevelPage(Integer page, Integer limit, String levelName) {
        Page<LevelInfo> levelIPage = new Page<>(page, limit);
        LambdaQueryWrapper<LevelInfo> queryWrapper = new LambdaQueryWrapper<LevelInfo>();
        if (StrUtil.isNotBlank(levelName)) {
            queryWrapper.like(LevelInfo::getLevelName, levelName);
        }
        queryWrapper.eq(LevelInfo::getDelFlg, '0');
        levelIPage = levelInfoMapper.selectPage(levelIPage, queryWrapper);
        levelIPage.setTotal(levelIPage.getRecords().size());
        return levelIPage;
    }

    @Override
    public LevelInfo queryLevelOne(Integer levelId) {
        LevelInfo levelInfo = new LevelInfo();
        if (levelId > 0){
            levelInfo = levelInfoMapper.selectById(levelId);
        }
        return levelInfo;
    }

    @Override
    public Integer insert(LevelInfo levelInfo) {
        return levelInfoMapper.insert(levelInfo);
    }

    @Override
    public boolean removeLevel(Integer levelId) {
        boolean flag = false;
        LevelInfo levelInfo = levelInfoMapper.selectById(levelId);
        if (Objects.nonNull(levelInfo)) {
            levelInfo.setDelFlg(1);
            Integer result = levelInfoMapper.updateById(levelInfo);
            if (result > 0) {
                flag = true;
            }
        }
        return flag;
    }
}
